package com.example.jdbconectiontest.config;

import lombok.SneakyThrows;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * 这里有个bug,如果不写这一步，MyMysqlPool无法正常加载配置文件，只能自己手动加载了
 */
@Component
public class MyBeanPostInitial implements BeanPostProcessor {
    @Value("${mysql.ip}")
    String mysqlIp;
    @Value("${mysql.port}")
    Integer mysqlPort;
    @Value("${mysql.password}")
    String mysqlPwd;
    @Value("${mysql.user}")
    String mysqlUser;
    @Value("${mysql.db}")
    String mysqlDB;
    @Value("${mysql.maxpoolSize}")
    private int size;
    @SneakyThrows
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof MyMysqlPool){
            bean=new MyMysqlPool(mysqlIp,mysqlPort,mysqlPwd,mysqlUser,mysqlDB,size);
        }
        return bean;
    }
}
